/*
 * @Author: i@douxuefeng.cn
 * @Date: 2022-01-22 15:14:01
 * @LastEditTime: 2022-01-22 15:42:04
 * @LastEditors: i@douxuefeng.cn
 * @Description:
 * @FilePath: \go-wechat\mp\serve\serve.go
 */

package serve

import (
	"sort"
	"strings"

	"gitee.com/douxuefeng/go-wechat/mp/mpconfig"
	"gitee.com/douxuefeng/go-wechat/mp/params"
	"gitee.com/douxuefeng/go-wechat/utils"
)

type serve struct {
	*mpconfig.MpConfig
}

func NewServe(cfg *mpconfig.MpConfig) *serve {
	return &serve{cfg}
}

func (mp *serve) CheckSignature(data params.Serve) string {
	var strs = []string{data.Timestamp, data.Nonce, mp.Token}
	sort.Slice(strs, func(i, j int) bool {
		return strs[i] < strs[j]
	})
	str := strings.Join(strs, "")
	if utils.Sha1(str) == data.Signature {
		return data.Echostr
	}
	return ""
}
